package com.xiaoyao.novel.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.crawl.BaseCrawl;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;

public class TaduParser {
	private static Logger logger = Logger.getLogger(TaduParser.class);

	public static void parseDescrib(String page, Book book) {
		page = page.replaceAll("\"", "'");
		String regex = "<div class='bookimg'>.*?<img.*?src='(.+?)'.+?作者.+?<em>(.+?)</em>.+?<p class='bookintro'>(.+?)</p>";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(page);
		if (matcher.find()) {
			book.authorName= matcher.group(2);
			book.describ=matcher.group(3);
			
			if (ClassUtil.isEmpty(book.picPath)) {
				String picUrl = matcher.group(1);
				String fileName = book.bookId + ".jpg";
				fileName = BaseCrawl.downLoadImage(picUrl, fileName); // 抓图片
				book.picPath = FileUtil.PIC_DIR + fileName;
			}
		}
	}

	public static List<Chapter> parseBook(String page, Book book) {
		logger.debug("开始解析...");
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		// page = page.replaceFirst(".+?正文", "");
		List<Chapter> list = new ArrayList<Chapter>();
		book.list = list;
		// <li><a title="更新时间:2013-9-27 6:55:19	更新字数:3453" target="_blank"
		// href="4770209.shtml">第一章 后悔井</a></li>
		String bookRegex = "<div class='chapter_t'>.+?<a.*?href='(.+?)'.*?title='(.+?)'.*?>(.+?)</a>\\s*</div>";
		Pattern bookPattern = Pattern.compile(bookRegex);
		// System.out.println(page);
		Matcher matcher = bookPattern.matcher(page);
		while (matcher.find()) {
			String title = matcher.group(2).trim();
			if (title.startsWith("引子") || title.startsWith("前言") || title.contains("第") || title.matches("\\d+.+")) {
				logger.debug(title);
				Chapter chapter = new Chapter();
				list.add(chapter);
				chapter.bookId = book.bookId;
				chapter.chapterUrl = matcher.group(1);
				chapter.chapterTitle = title;
				logger.debug(chapter);
			}
		}
		return list;
	}

	public static void parseChapter(String page, Chapter chapter) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		page = page.replaceAll("<script>.+?</script>", "");
		logger.debug("页面:" + page);
		// String chapterRegex =
		// "id='partContent'.*?>(.+?)</div>\\s*<div class='jump'>";
		String chapterRegex = "\\$\\('#partContent'\\)\\.html\\(unescape\\('(.+?)'";
		Pattern chapterPattern = Pattern.compile(chapterRegex);
		Matcher matcher = chapterPattern.matcher(page);
		if (matcher.find()) {// JS编码过
			String content = matcher.group(1);
			content = decodeFromHex(content);
			content = content.replaceAll("%3C", "<");
			content = content.replaceAll("%3E", ">");
			content = content.replaceAll("%2F", "/");
			chapter.chapterContent = content;
			logger.debug(chapter.chapterContent);
		}
	}
	public static String decodeFromHex(String input_str) {
		Pattern p = Pattern.compile("%u([a-zA-Z0-9]{4})");
		Matcher m = p.matcher(input_str);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, String.valueOf((char) Integer.parseInt(m.group(1), 16)));
		}
		m.appendTail(sb);
		return sb.toString();
	}

}
